package com.wm.wmim.connect.ws.service;
import com.wm.wmim.connect.sdk.push.bo.req.PushWsMessageReqBO;
import com.wm.wmim.connect.sdk.push.enums.WsPushResultEnum;
import com.wm.wmim.connect.sdk.push.enums.PushFailedReasonEnum;

import com.wm.wmim.connect.sdk.push.bo.resp.PushWsMessageFailedInfoBO;
import com.wm.wmim.connect.sdk.push.bo.resp.PushWsMessageRespBO;
import com.wm.wmim.connect.ws.manager.WebSocketSessionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weiming
 * @date 2024/10/19 14:09
 * @description
 */
@Slf4j
@Service
public class PushMessageService {

    public PushWsMessageRespBO pushMessageLocal(PushWsMessageReqBO reqBO) {
        Long userId = reqBO.getUserId();
        String message = reqBO.getMessage();
        String businessScene = reqBO.getBusinessScene();
        List<String> wsSessionIdList = reqBO.getWsSessionIdList();
        List<PushWsMessageFailedInfoBO> failedInfoBOList = new ArrayList<>();
        boolean hasSuccess = false;
        // 在当前机器的所有连接
        List<WebSocketSession> webSocketSessionList = WebSocketSessionManager.getSession(businessScene, userId);
        if (CollectionUtils.isNotEmpty(webSocketSessionList)) {
            for (WebSocketSession webSocketSession : webSocketSessionList) {
                wsSessionIdList.remove(webSocketSession.getId());
                boolean sendMessage = WebSocketSessionManager.sendMessage(webSocketSession, message);
                if (!sendMessage) {
                    // 发送失败
                    PushWsMessageFailedInfoBO failedInfoBO = new PushWsMessageFailedInfoBO();
                    if (!webSocketSession.isOpen()){
                        // 连接关闭
                        failedInfoBO.setFailedReason(PushFailedReasonEnum.CONNECT_NOT_EXIST);
                    } else {
                        failedInfoBO.setFailedReason(PushFailedReasonEnum.SEND_EXCEPTION);
                    }
                    failedInfoBO.setFailWsSessionId(webSocketSession.getId());
                    failedInfoBOList.add(failedInfoBO);
                } else {
                    hasSuccess = true;
                }
            }
        }
        // 连接不存在的, 说明断开了
        for (String sessionId : wsSessionIdList) {
            PushWsMessageFailedInfoBO failedInfoBO = new PushWsMessageFailedInfoBO();
            failedInfoBO.setFailedReason(PushFailedReasonEnum.CONNECT_NOT_EXIST);
            failedInfoBO.setFailWsSessionId(sessionId);
            failedInfoBOList.add(failedInfoBO);
        }
        PushWsMessageRespBO resultBO = new PushWsMessageRespBO();
        if (CollectionUtils.isEmpty(failedInfoBOList)){
            resultBO.setPushResult(WsPushResultEnum.ALL_SUCCESS);
        } else if (hasSuccess) {
            resultBO.setPushResult(WsPushResultEnum.PART_SUCCESS);
        } else {
            resultBO.setPushResult(WsPushResultEnum.ALL_FAIL);
        }
        resultBO.setFailedList(failedInfoBOList);
        return resultBO;
    }
}
